﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess.Advice
{
    public sealed class MoveAdvice:BaseAdvice
    {
        private ChessBoard _chessBoard;
        private Player _player;
        private ChessPiece _chessPiece;
        private Dir _dir;

        public MoveAdvice(ChessBoard chessBoard, Player player, ChessPiece chessPiece, Dir dir) {
            this._chessBoard = chessBoard;
            this._player = player;
            this._chessPiece = chessPiece;
            this._dir = dir;
        }

        public override int Execute()
        {
            AdviceStatusEnum status = AdviceStatusEnum.None;

            var pos = _chessBoard.GetChessPieceAround(_chessPiece, _dir);

            if (pos != null)
            {
                if (_chessBoard.IsEmpty(pos.Value))
                { //下一步棋位是空的
                    status = CompareAdviceStatus(status, AdviceStatusEnum.走棋_没吃子);
                }
                else if (_player.IsMyChessPiece(pos.Value)) 
                { //下一步棋位是自己的棋
                    status = CompareAdviceStatus(status, AdviceStatusEnum.走棋_走不通);
                }
                else 
                { //下一步棋位是对方的棋
                    //获得再下一步的棋位
                    var pos1 = _chessBoard.GetChessPieceAround(pos.Value,_dir);
                    if (pos1 != null && _chessBoard.IsEmpty(pos1.Value))
                    { //再下一步棋位是空的  
                        status = CompareAdviceStatus(status, AdviceStatusEnum.走棋_吃子);
                    }
                    else 
                    { //再下一步棋位有棋子
                        status = CompareAdviceStatus(status, AdviceStatusEnum.走棋_走不通);
                    }
                }               
            }
            else 
            { //下一步没有棋位
                status = CompareAdviceStatus(status, AdviceStatusEnum.走棋_走不通);
            }
            _score += GetScore(status);
            if (status == AdviceStatusEnum.走棋_吃子 || status == AdviceStatusEnum.走棋_没吃子)
            {
                return base.Execute();
            }
            else {
                return _score;
            }            
        }
    }
}
